<?php

namespace App\Models\Admin;

use App\Services\Utils\Bosable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;

/**
 * Class CustomerCompany
 *
 * @mixin Builder
 * @mixin \Illuminate\Database\Query\Builder
 *
 * @package App\Models\Admin
 */
class CustomerCompany extends Model
{
    use Bosable;

    private $customer_company_bos_key_prefix = 'customer_company';

    protected $table = 'customer_companies';
    protected $guarded = [];

    /**
     * @return mixed|null
     */
    public function getLogoAttribute()
    {
        if (!$this->attributes['logo']) return null;

        return $this->getStaticUrl($this->attributes['logo']);
    }

    /**
     * @param UploadedFile|string|null $logo
     * @throws \Exception
     */
    public function setLogoAttribute($logo)
    {
        if (is_file($logo)) {
            $this->attributes['logo'] = $this->uploadPostFile($logo, $this->customer_company_bos_key_prefix);
        } else {
            $this->attributes['logo'] = $logo;
        }
    }

    /**
     * @return mixed|null
     */
    public function getProductImageAttribute()
    {
        if (!$this->attributes['product_image']) return null;

        return $this->getStaticUrl($this->attributes['product_image']);
    }

    /**
     * @param UploadedFile|string|null $product_image
     * @throws \Exception
     */
    public function setProductImageAttribute($product_image)
    {
        if (is_file($product_image)) {
            $this->attributes['product_image'] = $this->uploadPostFile($product_image, $this->customer_company_bos_key_prefix);
        } else {
            $this->attributes['product_image'] = $product_image;
        }
    }
}
